package com.doradance.common.pay;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.doradance.app.MyApplication;


//import com.ahxmould.ring.app.MyApplication;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/10/13
 */

public class AlipayHandler extends Handler {
    PayResultCallback mCallback;

    public AlipayHandler(PayResultCallback callback) {
        mCallback = callback;
    }

    @Override
    public void handleMessage(Message msg) {
        PayResult payResult = new PayResult((String) msg.obj);
        String resultStatus = payResult.getResultStatus();
        String result = payResult.getResult();
        if (TextUtils.equals(resultStatus, "9000")) {
            Toast.makeText(MyApplication.getAppContext(), "支付成功", Toast.LENGTH_SHORT).show();
            mCallback.onSuccess();
        } else {
            // 判断resultStatus 为非“9000”则代表可能支付失败
            // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
            if (TextUtils.equals(resultStatus, "8000")) {

                Toast.makeText(MyApplication.getAppContext(), "支付结果确认中",
                        Toast.LENGTH_SHORT).show();


            } else {
                // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                Toast.makeText(MyApplication.getAppContext(), "支付失败",
                        Toast.LENGTH_SHORT).show();
                mCallback.onFail();
            }
        }
    }

}
